#include "Projectile.h"
#include "Player.h"
#include "Spawner.h"

#include <iostream>

Projectile::Projectile(int x, int y, int hDirection, int vDirection, Player* owner) :
	Movable(x, y, true, Type_Projectile),
	owner(owner)
{
	hVelocity = hDirection;
	vVelocity = vDirection;

	moveInterval = 100;
}


Projectile::~Projectile()
{
	if (owner)
		owner->projectileDestroyed(this);
}


void Projectile::getTimedDesiredMove(int& hDirection, int& vDirection)
{
	getVelocity(hDirection, vDirection);
}


bool Projectile::isPassable(const GameObject* gameObject) const
{
	return gameObject->getType() == Type_Projectile;
}


void Projectile::handleCollision(GameObject* gameObject)
{
	if (!gameObject)
	{
		// Projectile left the map.
		destroy();
		return;
	}

	// Kill switch.
	switch (gameObject->getType())
	{
	case Type_Monster: // Kill monsters.
		if (owner)
			std::cout << owner->getName() << " killed a monster! " << std::endl;
		gameObject->destroy();
		break;
	case Type_Player:
		if (gameObject != owner) 
		{ 
			// Kill players that are not the owner.
			gameObject->destroy();
		}
		break;
	}
	 
	// Exceptions to destruction on collision.
	if (gameObject->getType() == Type_Projectile || gameObject->getType() == Type_PickUp)
	{
		return;
	}
	
	destroy();
}


void Projectile::setDrawColor() const 
{
	setColor(1.0f, 1.0f, 1.0f);
}

void Projectile::clearOwner()
{
	owner = 0;
}

void Projectile::drawObject(int row, int col) const
{
	std::vector<drawPoint> points;
	
	setDrawColor();

	// Setup for each direction below.
	if (hVelocity == 1) // Right.
	{
		points.push_back(drawPoint(col      , row + 0.2, 0));
		points.push_back(drawPoint(col + 0.8, row + 0.2, 0));
		points.push_back(drawPoint(col + 1  , row + 0.5, 0));
		points.push_back(drawPoint(col + 0.8, row + 0.8, 0));
		points.push_back(drawPoint(col      , row + 0.8, 0));
	}
	else if (hVelocity == -1) // Left.
	{
		points.push_back(drawPoint(col + 0.2, row + 0.2, 0));
		points.push_back(drawPoint(col + 1  , row + 0.2, 0));
		points.push_back(drawPoint(col + 1  , row + 0.8, 0));
		points.push_back(drawPoint(col + 0.2, row + 0.8, 0));
		points.push_back(drawPoint(col      , row + 0.5, 0));
	}
	else if (vVelocity == 1) // Down.
	{
		points.push_back(drawPoint(col + 0.2, row      , 0));
		points.push_back(drawPoint(col + 0.8, row      , 0));
		points.push_back(drawPoint(col + 0.8, row + 0.8, 0));
		points.push_back(drawPoint(col + 0.5, row + 1  , 0));
		points.push_back(drawPoint(col + 0.2, row + 0.8, 0));
	}
	else if (vVelocity == -1) // Up.
	{
		points.push_back(drawPoint(col + 0.2, row + 0.2, 0));
		points.push_back(drawPoint(col + 0.5, row      , 0));
		points.push_back(drawPoint(col + 0.8, row + 0.2, 0));
		points.push_back(drawPoint(col + 0.8, row + 1  , 0));
		points.push_back(drawPoint(col + 0.2, row + 1  , 0));
	}



	drawInMode(GL_POLYGON, points);
	points.clear();
}